package com.simtop.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;

import com.simtop.pojo.User;
import com.simtop.pojo.exchange.Question;
import com.simtop.pojo.wddata.MainTain;
import com.simtop.pojo.wddata.WdDate;
import com.simtop.pojo.wddata.api;
import com.simtop.service.ApiService;
import com.simtop.util.JwtUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/device")
@CrossOrigin
public class DeviceController {

    @Autowired
    private ApiService apiService;

    //获取维护
    @PostMapping("/viewData")
    public ServerResponse rtdata(@RequestBody String data, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer pageNum = jsonObject.getInteger("pageNum");
            Integer pageSize = jsonObject.getInteger("pageSize");
            if (StringUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (StringUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            PageHelper.startPage(pageNum, pageSize);
            //查询表中所有数据
            String column = jsonObject.getString("column");
            String order = jsonObject.getString("order");
            String deviceName = jsonObject.getString("deviceName");
            String deviceType = jsonObject.getString("deviceType");
            List<MainTain> findAlldeviceList = apiService.findAlldeviceList(column, order,deviceName,deviceType);
            for (MainTain as:findAlldeviceList
                 ) {
                Date date2 = as.getMainTime();
                Date date1 = new Date();
                int i = differentDaysByMillisecond(date1, date2);
                as.setSafeTimetoday(i+1);

                Date date4 = as.getCreateTime();
                Date date3 = as.getSafeTime();
                int is = differentDaysByMillisecond(date4, date3);
                as.setBeginTime(is);

            }
            PageInfo<MainTain> pageInfo = new PageInfo<MainTain>(findAlldeviceList);
            return ServerResponse.createBySuccess("获取内容成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }

    @PostMapping("/geTtimeCount")
    public ServerResponse geTtimeCount(@RequestBody String data, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer timeCount = jsonObject.getInteger("timeCount");
            apiService.timeCount(timeCount);


            return ServerResponse.createBySuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }

    @PostMapping("/findTtimeCount")
    public ServerResponse findTtimeCount( HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {

            Integer findTtimeCount = apiService.findTtimeCount();


            return ServerResponse.createBySuccess(findTtimeCount);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }
    public static int differentDaysByMillisecond(Date date1,Date date2)
    {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
        return days;
    }


    @PostMapping("/findviewData")
    @ResponseBody
    public ServerResponse findviewData( HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {

            List<String> as = apiService.findviewData();





            return ServerResponse.createBySuccess(as);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }



    //搜索维护
    @PostMapping("/soutviewData")
    public ServerResponse soutviewData(@RequestBody String data, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer pageNum = jsonObject.getInteger("pageNum");
            Integer pageSize = jsonObject.getInteger("pageSize");
            if (StringUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (StringUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            PageHelper.startPage(pageNum, pageSize);
            //查询表中所有数据
            String deviceName = jsonObject.getString("deviceName");
            String deviceType = jsonObject.getString("deviceType");
            List<MainTain> findAlldeviceList2 = apiService.findAlldeviceList2(deviceName, deviceType);
            PageInfo<MainTain> pageInfo = new PageInfo<MainTain>(findAlldeviceList2);
            return ServerResponse.createBySuccess("获取内容成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }

    //修改设备维护
    @PostMapping("/upDateViewData")
    public ServerResponse upDateViewData(@RequestBody String data, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            JSONArray devicepoliceOne = jsonObject.getJSONArray("devicepoliceOne");
            String devicepolicelistOne = String.valueOf(devicepoliceOne);
            JSONArray devicepoliceTwo = jsonObject.getJSONArray("devicepoliceTwo");
            String devicepolicelistTwo = String.valueOf(devicepoliceTwo);
            JSONArray devicepoliceThree = jsonObject.getJSONArray("devicepoliceThree");
            String devicepolicelistThree = String.valueOf(devicepoliceThree);
            JSONArray devicepoliceFor = jsonObject.getJSONArray("devicepoliceFor");
            String devicepolicelistFor = String.valueOf(devicepoliceFor);
            Integer deviceId = jsonObject.getInteger("deviceId");
            String testLocation = jsonObject.getString("testLocation");
            Integer deviceNormalup = jsonObject.getInteger("deviceNormalup");
            Integer deviceNormaldown = jsonObject.getInteger("deviceNormaldown");
            Integer devicePoliceup = jsonObject.getInteger("devicePoliceup");
            Integer devicePolicedown = jsonObject.getInteger("devicePolicedown");
            String testType = jsonObject.getString("testType");
            Date mainTime = null;
            mainTime = jsonObject.getDate("mainTime");
            if(mainTime != null){
                apiService.changemaintime(deviceId,mainTime);
            }

            //更新
            apiService.updateDevice(deviceId, devicepolicelistOne, devicepolicelistTwo, devicepolicelistThree, devicepolicelistFor
                    , testLocation, deviceNormalup, deviceNormaldown, devicePoliceup, devicePolicedown, testType );

            return ServerResponse.createBySuccess("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }


    //报警控制接口
    @PostMapping("/bjController")
    public ServerResponse bjController(@RequestBody String data, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer policeControl = jsonObject.getInteger("policeControl");
            String testType = jsonObject.getString("testType");
            Integer deviceId =jsonObject.getInteger("deviceId");
            apiService.updeviceStatus(policeControl,testType,deviceId);


            return ServerResponse.createBySuccess("成功");

        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }

    //预警控制接口
    @PostMapping("/yjController")
    public ServerResponse yjController(@RequestBody String data, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ") + 1);
        User u = JwtUtil.unsign(jwt, User.class);
        if (u == null) {
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer yjControl = jsonObject.getInteger("yjControl");
            String testType = jsonObject.getString("testType");
            Integer deviceId =jsonObject.getInteger("deviceId");
            apiService.yjController(yjControl,testType,deviceId);


            return ServerResponse.createBySuccess("成功");

        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }
}
